//
//  DataRequest.m
//  AppleCilent
//
//  Created by Nail Sharipov on 02/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "DataRequest.h"

@implementation DataRequest
{
    DataReadyHandler dataReadyHandler;
    NSMutableData* receivedData;
}

-(void)getURL: (NSURL*)url onReady: (DataReadyHandler) onReadyHandler
{
    NSLog(@"sendURL");
    dataReadyHandler = onReadyHandler;
    NSURLRequest* request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:10.0];

    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (connection)
    {
        receivedData = [[NSMutableData alloc] init];
    }
    NSLog(@"request to %@", url);
}

-(void)sendToURL: (NSURL*)url parameters:(NSString*)params onReady: (DataReadyHandler) onReadyHandler
{
    NSLog(@"sendToURL");
    dataReadyHandler = onReadyHandler;
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:10.0];
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (connection)
    {
        receivedData = [[NSMutableData alloc] init];
    }
    
    NSLog(@"request to %@", url);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
    NSLog(@"server ready");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
    NSLog(@"receive data");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSString *errorString = [[NSString alloc] initWithFormat:@"Connection failed! Error - %@ %@ %@",
                             [error localizedDescription],
                             [error description],
                             [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];
    NSLog(@"error: %@", errorString);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    dataReadyHandler(receivedData);
    NSLog(@"loaded");
}

-(void)downloadURL: (NSURL*)url fileName: (NSString*)fileName folder: (NSString*)folder onReady: (DownloadFinishedHandler) onReadyHandler
{
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:urlRequest
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response, NSData *responseData, NSError *error)
    {
        
        if (responseData)
        {
            NSString *filePath = [folder stringByAppendingPathComponent:fileName];
            [responseData writeToFile:filePath atomically:YES];
            onReadyHandler(filePath);
        }
                               
    }];

}



@end
